<?php
namespace app\api\service;
use think\Request;
use app\api\model\Finished;
use app\api\model\Category;
use app\api\model\Dc;
class FinishedService {

    public function page($page, $size){
        $data 	= Request::instance()->get();
        $where 	= [];
        empty($data['name']) || $where['name'] = ['like','%'.$data['name']];
        empty($data['type_id']) || $where['type_id'] = $data['type_id'];
        
        $obj = Finished::with(['ftype'])->where($where)
        ->paginate($size, false, ['page' => $page]);

        if ($obj->isEmpty()){
            return [];
        }
        return $obj->toArray();
    }

    public function all(){
        $obj = Category::with(['finished'])->where('type', 2)->select();
        return show(1, 'ok', $obj, 200);
    }

    public function save(){ 
        $param = Request::instance()->param();	//获取参数

        if (!is_numeric($param['id']) || !isset($param['id']))
        {
            // 新增
            $obj = new Finished;

            try {
                $obj->name = $param['name'];
                $obj->specification = $param['specification'];
                $obj->type_id = $param['type_id'];
                $obj->img = $param['img'];
                $obj->price = $param['price'];
            }catch(Exption $e) {
                return show(0, '修改失败', [], 200);
            }

            if( $obj->save()){
                return show(1, '新增成功', [], 200);
            }
        }else {
            // 更新
            $obj = Finished::get($param['id']);
            try {
                $obj->name = $param['name'];
                $obj->specification = $param['specification'];
                $obj->type_id = $param['type_id'];
                $obj->img = $param['img'];
                $obj->price = $param['price'];
            }catch(Exception $e) {
                return show(0, '修改失败', [], 200);
            }

            // 检测错误
            if( $obj->save()){
                return show(1, '修改成功', [], 200);
            }
        }
        
        return show(0, '修改失败', [], 200);
    }

    public function delete($id){

    	if( Finished::destroy($id, true) ){
            return show(1, '删除成功', [], 200);
        }
        
        return show(0, '删除失败', [], 200);
    }

    // 获取当前店铺 线上销售商品及数量
    public function showNum() {

        $data = Request::instance()->get();
        // 获取全部商品
        $obj = Finished::select();
        
        // 获取当前店铺商品数量
        $dx = Dc::where('alliance_id', $data['alliance_id'])->select();

        foreach($obj as $key => $item) {
            $obj[$key]['num'] = 0;
            for ($i=0; $i < count($dx); $i++) { 
                if ($dx[$i]['finished_id'] == $item['id']) {
                    $obj[$key]['num'] = $dx[$i]['num'];
                    continue;
                }
            }
        }

        return show(1, 'ok', $obj, 200);
    }

    // 修改数量 参数店铺id 商品id
    public function setNum() {
        $param = Request::instance()->param();	//获取参数
        // 
        $res = Dc::where('finished_id', $param['finished_id'])
        ->where('alliance_id', $param['alliance_id'])
        ->find();

        if (!$res) {
            $obj = new Dc;

            try {
                $obj->finished_id = $param['finished_id'];
                $obj->alliance_id = $param['alliance_id'];
                $obj->num = $param['num'];
            }catch(Exption $e) {
                return show(0, '修改失败', [], 200);
            }
            if( $obj->save()){
                return show(1, '修改成功', [], 200);
            }
        }

        $obj = Dc::get($res['id']);

        try {
            $obj->num = $param['num'];
        }catch(Exception $e) {
            return show(0, '修改失败', [], 200);
        }

        // 检测错误
        if( $obj->save()){
            return show(1, '修改成功', [], 200);
        }

        return show(0, '修改失败', [], 200);
    }

    // 获取当前分类下的商品及数量
    public function getProduct($page, $size) {
        $data = Request::instance()->get();
        $obj = Finished::where('type_id', $data['type_id'])->limit(($page - 1) * $size, $size)->select();
        
         // 获取当前店铺商品数量
         $dx = Dc::where('alliance_id', $data['alliance_id'])->select();

         foreach($obj as $key => $item) {
            $obj[$key]['num'] = 0;
            for ($i=0; $i < count($dx); $i++) { 
                if ($dx[$i]['finished_id'] == $item['id']) {
                    $obj[$key]['num'] = $dx[$i]['num'];
                    continue;
                }
            }
        }

        return show(1, 'ok', $obj, 200);
    }
}